﻿#include "thread_rdwr.h"
#include <QDebug>
#include <QFile>

Thread_RDWR::Thread_RDWR(QTcpServer* ser_tcpcp, QTcpSocket* ser_t, QHash<QString, QTcpSocket*> ser_hash, QVector<QString> id_,QVector<QString> passwd_, QVector<QString> name_,QObject *parent) : QThread(parent)
{
    m_s = ser_tcpcp;
    ser_tcp = ser_t;
    hash = ser_hash;
    id = id_;
    passwd = passwd_;
    name = name_;
}

void Thread_RDWR::run()
{
    qDebug() << "Thread: " << QThread::currentThread();
    ser_tcp = m_s->nextPendingConnection();
    connect(ser_tcp, &QTcpSocket::readyRead, this, [=](){
        //数据分类
        int type_apt;
        ser_tcp->read((char*)&type_apt, 4);
        if (type_apt == 1) {
            int size = 0;
            ser_tcp->read((char*)&size, 4);
            qDebug() << size;
            char id_[20]={0};
            char passwd_[20]={0};
            ser_tcp->read(id_, size);
            ser_tcp->read((char *)&size, 4);
            ser_tcp->read(passwd_, size);
            QString uid(id_);
            QString upasswd(passwd_);
            bool flag = false;
            int type = 1;
            ser_tcp->write((char*)&type, 4);
            for (int i = 0;i < id.size(); i++) {
                if (QString::compare(id[i], uid)==0 && QString::compare(passwd[i], upasswd) == 0) {
                    qDebug() << "passwd ok";
                    sendHashData(uid, ser_tcp);
                    flag = true;
                    ser_tcp->write("ok");
                    break;
                }
            }
            //帐号或密码不正确
            if (!flag) {
                ser_tcp->write("no");
            }
        } else if (type_apt == 2) {
            int name_len;
            int passwd_len;
            char name_[20]={0};
            char passwd_[20]={0};
            ser_tcp->read((char*)&name_len, 4);
            ser_tcp->read(name_, name_len);
            ser_tcp->read((char*)&passwd_len, 4);
            ser_tcp->read(passwd_, passwd_len);
            QString uname(name_);
            QString upasswd(passwd_);
            //生成帐号
            int k, i;
            QString uid;
            for (k = 10000;k < 99999; k++) {
                uid = QString::number(k, 10);
                for (i=0;i < id.size(); i++) {
                    if (QString::compare(uid, id[i]) == 0) {
                        break;
                    }
                }
                if (i == id.size()) {
                    break;
                }
            }
            int type = 2;
            ser_tcp->write((char*)&type, 4);
            ser_tcp->write(uid.toUtf8());
            emit sendUsrData(uid, upasswd, uname, 1);

        } else if (type_apt == 3) {
            int id_len;
            int new_len;
            char id_[20]={0};
            char new_[20]={0};
            ser_tcp->read((char*)&id_len, 4);
            ser_tcp->read(id_, id_len);
            ser_tcp->read((char*)&new_len, 4);
            ser_tcp->read(new_, new_len);
            QString uid(id_);
            QString unew(new_);
            int i;
            qDebug() << "ID = " << uid << "len = " << id_len;
            qDebug() << "NPD = " << unew << "len = " << new_len;
            QString id_tmp = uid;
            QString name_tmp;
            for (i=0;i < id.size();i++) {
                if (QString::compare(uid, id[i]) == 0) {
                    break;
                }
            }
            int type = 3;
            ser_tcp->write((char*)&type, 4);
            if (i == id.size()) {
                ser_tcp->write("nofind");
            } else {
                ser_tcp->write("ok");
                emit sendUsrData(id_tmp, unew, name_tmp, 2);
            }

        } else if (type_apt == 4) {
            //更新当前在线人数
            emit updateBroList(ser_tcp);
        } else if (type_apt == 5) {
            int to_len;
            int from_len;
            char from_id_[20]={0};
            char to_id_[20]={0};
            ser_tcp->read((char*)&from_len, 4);
            ser_tcp->read(from_id_, from_len);
            ser_tcp->read((char*)&to_len, 4);
            ser_tcp->read(to_id_, to_len);
            QByteArray msg_ = ser_tcp->readAll();
            QString from_id(from_id_);
            QString to_id(to_id_);
            QString msg = msg_;
            emit sendId_Msg(from_id, to_id, msg);
        } else if (type_apt == 6) {
            int to_len;
            int from_len;
            int data_len;
            char from_id_[20]={0};
            char to_id_[20]={0};
            char data_[1024]={0};
            ser_tcp->read((char*)&from_len, 4);
            ser_tcp->read(from_id_, from_len);
            ser_tcp->read((char*)&to_len, 4);
            ser_tcp->read(to_id_, to_len);
            ser_tcp->read((char*)&data_len, 4);
            ser_tcp->read(data_, data_len);
            QString from_id(from_id_);
            QString to_id(to_id_);
            QString data(data_);
            qDebug() << "data: "+ data;
            emit sendFileContent(from_id, to_id, data_len, data);
            QByteArray all_ = ser_tcp->readAll();
            QString all = all_;
            emit sendAll(to_id, all);

        } else if (type_apt == 7) {
            int to_len;
            int from_len;
            char from_id_[20]={0};
            char to_id_[20]={0};
            ser_tcp->read((char*)&from_len, 4);
            ser_tcp->read(from_id_, from_len);
            ser_tcp->read((char*)&to_len, 4);
            ser_tcp->read(to_id_, to_len);
            QByteArray msg_ = ser_tcp->readAll();
            QString from_id(from_id_);
            QString to_id(to_id_);
            QString msg = msg_;
            emit sendGroupMsg(from_id, to_id, msg);
        } else if (type_apt == 8) {
            int id_len;
            char tp_id[20]={0};
            ser_tcp->read((char*)&id_len, 4);
            ser_tcp->read(tp_id, id_len);
            QString tp_(tp_id);
            emit sendTp(tp_);
        } else if (type_apt == 9) {
            int id_len;
            char noSay_id[20]={0};
            ser_tcp->read((char*)&id_len, 4);
            ser_tcp->read(noSay_id, id_len);
            QString noSay_(noSay_id);
            emit sendNoSay(noSay_);
        } else if (type_apt == 10) {
            int id_len;
            char openSay_id[20]={0};
            ser_tcp->read((char*)&id_len, 4);
            ser_tcp->read(openSay_id, id_len);
            QString openSay(openSay_id);
            emit sendOpenSay(openSay);
        }

        //断开连接回收资源
        connect(ser_tcp, &QTcpSocket::disconnected, this, [=](){
            emit over(ser_tcp);
        });

    });

}
